// 用common.js必须加上Feng.addCtx("${ctxPath}");

Feng.info = function (info) {
    top.layer.msg(info, {icon: 6});
};
Feng.success = function (info) {
    top.layer.msg(info, {icon: 1});
};
Feng.error = function (info) {
    top.layer.msg(info, {icon: 2});
};
Feng.confirm = function (tip, ensure) {
    top.layer.confirm(tip, {
        skin: 'layui-layer-admin'
    }, function ()  {
        ensure();
    });
};

/**
 * * 将数值四舍五入(保留2位小数)后格式化成金额形式
 * *
 * * @param num 数值(Number或者String)
 * * @return 金额格式的字符串,如'1,234,567.45'
 * * @type String
 * */

Feng.formatCurrency=function (num) {

    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + '.' + cents);


}
//元素判空
Feng.isEmpty=function(e){
    if(typeof e === 'undefined'){
        return false;
    }
    if(null===e){
        return false;
    }
    if(e==""){
        return false;
    }
    return true;

}
//两个时间相差天数 兼容firefox chrome

Feng.datedifference=function(sDate1, sDate2) {    //sDate1和sDate2是2006-12-18格式
    var dateSpan,
        tempDate,
        iDays;
    sDate1 = Date.parse(sDate1);
    sDate2 = Date.parse(sDate2);
    dateSpan =sDate1-sDate2;
    //dateSpan = Math.abs(dateSpan);
    iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
    return iDays
};
/**
 * 是否大于十分钟
 * @param now
 */

Feng.isAfterNow=function(newTime) {    //sDate1和sDate2是2006-12-18格式
    var dateSpan,
        tempDate,
        iDays;
    var sDate1 = new Date();
    var sDate2 = Date.parse(newTime);
    dateSpan =sDate1-sDate2;
    //dateSpan = Math.abs(dateSpan);
    //iDays = Math.floor(dateSpan / (2 *60* 1000));
    if(dateSpan>0){
        return  false;
    }

    return ttue;
};

/**
 * 状态匹配
 * @param obj 所有情况
 * @param goal 目标
 * @param res  无匹配时的返回
 * @returns {string}
 *
 */

Feng.MatchStatus=function (obj,goal,res) {
    for(var i=0;i<obj.length; i++){
        if(obj[i][0]==goal){
            return obj[i][1];
        }
    }
    return res;
}
/**
 * 根据不同状态值返回不同背景颜色
 * @param val 状态值 数字 小于10最好 如果时间大于10 小于100 取十位数的数字
 * @param show 显示的内容
 * @returns {string}
 */
Feng.addBackColor4Enum=function(val,show){


    if(val =="" && val!= 0 ){
        return "无数据";
    }else if(val =="0"){
        return '<div><p style="height: 40px; width:100px;color:white;  text-align: center;background-color:#FF5722">'+ show+'</p></div>';
    }
    var number = parseInt(val);
    if(number>=10&&number<100){
        val=number/10
    }

    var color;

    if (val==1){
        color="#2F4056";
    }else if (val==2){
        color="#FFB800";
    }else if (val==3){
        color="#FF5722"
    }else if (val==4){
        color="#01AAED"
    }else if (val==5){
        color="#009688"
    }else if (val==6){
        color="#5FB878"
    }else if (val==7){
        color="#0078Bf";
    }
    return '<div><p style="height: 40px; width:100px;color:white;  text-align: center;background-color:'+ color+'">'+ show+'</p></div>'

}

/**
 * 获得当前日期
 * @returns {string}
 */
Feng.currentDate = function () {
    // 获取当前日期
    var date = new Date();

    // 获取当前月份
    var nowMonth = date.getMonth() + 1;

    // 获取当前是几号
    var strDate = date.getDate();

    // 添加分隔符“-”
    var seperator = "-";

    // 对月份进行处理，1-9月在前面添加一个“0”
    if (nowMonth >= 1 && nowMonth <= 9) {
        nowMonth = "0" + nowMonth;
    }

    // 对月份进行处理，1-9号在前面添加一个“0”
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }

    // 最后拼接字符串，得到一个格式为(yyyy-MM-dd)的日期
    return date.getFullYear() + seperator + nowMonth + seperator + strDate;
};

Feng.getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    } else {
        return null;
    }
};


Feng.infoDetail = function (title, info) {
    var display = "";
    if (typeof info === "string") {
        display = info;
    } else {
        if (info instanceof Array) {
            for (var x in info) {
                display = display + info[x] + "<br/>";
            }
        } else {
            display = info;
        }
    }

    top.layer.open({
        title: title,
        type: 1,
        skin: 'layui-layer-rim', //加上边框
        area: ['950px', '500px'], //宽高
        content: '<div style="padding: 20px;" class="sdf">' + display + '</div>'
    });
};
/**
 * 单选框的加载 最后要与form的render方法一起用
 * @param tagName 便签的name属性名
 * @param val 要渲染的值
 */



Feng.zTreeCheckedNodes = function (zTreeId) {
    var zTree = $.fn.zTree.getZTreeObj(zTreeId);
    var nodes = zTree.getCheckedNodes();
    var ids = "";
    for (var i = 0, l = nodes.length; i < l; i++) {
        ids += "," + nodes[i].id;
    }
    return ids.substring(1);
};
Feng.closeAllLoading = function () {
    layer.closeAll('loading');
};

//
/**
 *
 * @param component 元素
 * @param status true :使无效 ,false 移除disabled
 * 使某个组件无效 disable
 */
Feng.disableComponent = function (component,status ) {
   if(Feng.isEmpty(component)){
       if(component.length>1){
           for(var i=0;i<component.length;i++){
               status?component[i].setAttribute("disabled","disabled"):component[i].removeAttribute("disabled")
           }
       }else {
           status?component.setAttribute("disabled","disabled"):component.removeAttribute("disabled")
       }
   }
};

//按钮的渲染
/**
 *
 * @param name 标签name的值
 * @param val 目标值
 */
Feng.buttonInit=function( name,val){
    var sta = document.getElementsByName(name);
    if(Feng.isEmpty(sta)){
        return;
    }
    for(var i=0;i<sta.length;i++){
        if(sta[i].value==val){
            sta[i].setAttribute("checked","checked");
        }
    }
}



// 以下代码是配置layui扩展模块的目录，每个页面都需要引入
layui.config({
    base: Feng.ctxPath + '/assets/common/module/'
}).extend({
    formSelects: 'formSelects/formSelects-v4',
    treetable: 'treetable-lay/treetable',
    dropdown: 'dropdown/dropdown',
    notice: 'notice/notice',
    step: 'step-lay/step',
    dtree: 'dtree/dtree',
    citypicker: 'city-picker/city-picker',
    tableSelect: 'tableSelect/tableSelect',
    ax: 'ax/ax',
    ztree: 'ztree/ztree-object',
    simTool: 'extends/simtool'

}).use(['admin'], function () {
    var $ = layui.$;
    var admin = layui.admin;

    // 单标签模式需要根据子页面的地址联动侧边栏的选中，用于适配浏览器前进后退按钮
    if (window != top && top.layui && top.layui.index && !top.layui.index.pageTabs) {
        top.layui.admin.activeNav(location.href.substring(Feng.ctxPath.length));
    }

    // 移除loading动画
    setTimeout(function () {
        admin.removeLoading();
    }, window == top ? 300 : 150);

    //注册session超时的操作
    $.ajaxSetup({
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        complete: function (XMLHttpRequest, textStatus) {

            //通过XMLHttpRequest取得响应头，sessionstatus，
            var sessionstatus = XMLHttpRequest.getResponseHeader("sessionstatus");
            if (sessionstatus === "timeout") {

                //如果超时就处理 ，指定要跳转的页面
                window.location = Feng.ctxPath + "/global/sessionError";
            }
        }
    });





});